# Makefile for Version Module
# 104协议库版本模块

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
AR = ar
ARFLAGS = rcs

# 目录设置
SRC_DIR = src
INCLUDE_DIR = include
BUILD_DIR = ../../../output
OBJ_DIR = $(BUILD_DIR)/obj/version
LIB_DIR = $(BUILD_DIR)/lib

# 包含路径
INCLUDES = -I$(INCLUDE_DIR)

# 源文件
SOURCES = $(wildcard $(SRC_DIR)/*.c)

# 目标文件
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

# 库文件
STATIC_LIB = $(LIB_DIR)/libversion.a
SHARED_LIB = $(LIB_DIR)/libversion.so

# 默认目标
all: directories $(STATIC_LIB) $(SHARED_LIB)

# 创建目录
directories:
	@mkdir -p $(OBJ_DIR)
	@mkdir -p $(LIB_DIR)

# 静态库
$(STATIC_LIB): $(OBJECTS)
	@echo "Creating static library: $@"
	$(AR) $(ARFLAGS) $@ $^

# 共享库
$(SHARED_LIB): $(OBJECTS)
	@echo "Creating shared library: $@"
	$(CC) -shared -o $@ $^

# 编译目标文件
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 清理
clean:
	@echo "Cleaning version module..."
	rm -rf $(OBJ_DIR)

# 安装
install: $(STATIC_LIB) $(SHARED_LIB)
	@echo "Installing version module..."
	@mkdir -p /usr/local/include/protocol104/version
	@cp $(INCLUDE_DIR)/*.h /usr/local/include/protocol104/version/
	@cp $(STATIC_LIB) /usr/local/lib/
	@cp $(SHARED_LIB) /usr/local/lib/

# 卸载
uninstall:
	@echo "Uninstalling version module..."
	@rm -rf /usr/local/include/protocol104/version
	@rm -f /usr/local/lib/libversion.*

# 帮助
help:
	@echo "Available targets:"
	@echo "  all      - Build static and shared libraries"
	@echo "  clean    - Remove build files"
	@echo "  install  - Install libraries and headers"
	@echo "  uninstall- Remove installed files"
	@echo "  help     - Show this help"

.PHONY: all clean install uninstall help directories
